/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChooseOponentDialog.java
 *
 * Created on 03/10/2009, 15:58:54
 */

package gomoku;

import gomoku.NetworkAdapter.GoMokuGameLogic;
import java.awt.event.ActionEvent;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Slim
 */
public class ChooseOponentDialog extends javax.swing.JDialog {

    private String m_selectedOponent;
    private String[] playerItems;

    /** Creates new form ChooseOponentDialog */
    public ChooseOponentDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        m_selectedOponent = "";
    }

    public void setPlayers(String players)
    {
         playerItems = players.split(GoMokuGameLogic.PROTOCOL_CLIENT_SEPARATOR);
         DefaultListModel list = new DefaultListModel();
         for (int currPlayer = 0; currPlayer < playerItems.length; currPlayer++)
         {
             String[] player = playerItems[currPlayer].split(" ");
             list.addElement(String.format("%s   (ip:%s  id:%s)", player[1], player[2].substring(2, player[2].length()-1), player[0]));
         }
         lstPlayers.setModel(list);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstPlayers = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane2.setViewportView(jTable1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gomoku.GoMokuApp.class).getContext().getResourceMap(ChooseOponentDialog.class);
        setTitle(resourceMap.getString("ChoseOponentDialog.title")); // NOI18N
        setModal(true);
        setName("ChoseOponentDialog"); // NOI18N
        setResizable(false);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        lstPlayers.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lstPlayers.setToolTipText(resourceMap.getString("lstPlayers.toolTipText")); // NOI18N
        lstPlayers.setName("lstPlayers"); // NOI18N
        lstPlayers.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                lstPlayersMousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(lstPlayers);

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE)
                .addComponent(jButton2))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(278, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(29, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        choseOponent();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void choseOponent()
    {
        // No selection made
        if (lstPlayers.getSelectedIndex() == -1)
        {
            JOptionPane.showMessageDialog(this, "Please chose an oponent", "GoMoku Game", JOptionPane.ERROR_MESSAGE);
        }
        else
        {
            m_selectedOponent = playerItems[lstPlayers.getSelectedIndex()];
            this.setVisible(false);
        }
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        m_selectedOponent = "";
        this.setVisible(false);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void lstPlayersMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lstPlayersMousePressed
        if (evt.getClickCount() == 2 && evt.getButton() == evt.BUTTON1)
        {
            choseOponent();
        }
    }//GEN-LAST:event_lstPlayersMousePressed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ChooseOponentDialog dialog = new ChooseOponentDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JList lstPlayers;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the m_selectedOponent
     */
    public String getOponent() {
        return m_selectedOponent;
    }

}
